package com.leetcode.LC_MED;

import java.util.ArrayList;

public class LC1267 {
    public int countServers(int[][] grid) {
        ArrayList<Integer[]> list = new ArrayList<>();
        int sumCount = 0;
        for (int i = 0; i < grid.length; i++) {
            int x = -1, y = -1, count = 0;
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    x = i;
                    y = j;
                    count++;
                    sumCount++;
                }
            }
            if (count == 1) {
                Integer[] integers = {x, y};
                list.add(integers);
            }
        }
        int CanNotRes = 0;
        for (Integer[] integers : list) {
            int cnt = 0;
            for (int i = 0; i < grid.length; i++) {
                if (grid[i][integers[1]] == 1) {
                    cnt++;
                }
            }
            if (cnt == 1) {
                CanNotRes++;
            }
        }
        return sumCount - CanNotRes;
    }
}
